package cn.itsource.system.controller;

import cn.itsource.system.domain.Logininfo;
import cn.itsource.system.query.LogininfoQuery;
import cn.itsource.system.service.ILogininfoService;
import cn.itsource.util.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 接口类：登录信息Logininfo
 */
@RestController
@RequestMapping(value = "/logininfo")
@Api(tags = "登录信息接口类")
public class LogininfoController {
    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    public List<Logininfo> findAll(){
        return logininfoService.findAll();
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Logininfo findOne(@PathVariable("id")Long id){
        return logininfoService.findOne(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id")Long id){
        try {
            logininfoService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 添加或修改
     * @param logininfo
     * @return
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Logininfo logininfo){
        try {
           if(logininfo.getId()==null){
               logininfoService.add(logininfo);
           }else{
               logininfoService.update(logininfo);
           }
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public JsonResult patchDel(@RequestBody Long[] ids){
        try {
            logininfoService.patchDel(ids);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 分页查询+高级查询
     * @param logininfoQuery
     * @return
     */
    @PostMapping
    public PageInfo<Logininfo> queryPage(@RequestBody LogininfoQuery logininfoQuery){
        return logininfoService.queryPage(logininfoQuery);
    }

}
